#include "StompedReality/Components/PatchComponent.h"
#include "StompedReality/Components/RenderInfoComponent.h"
#include "StompedReality/Core/Game.h"
#include "StompedReality/Core/Memory/ObjectPool.h"
#include "StompedReality/Core/Memory/ObjectPoolManager.h"
#include "StompedReality/Core/MessageSystem/Message.h"
#include "StompedReality/Core/MessageSystem/MessageTypes.h"
#include <UnitTest++.h>

static const StompedReality::ID NEW_OBJECT_ID_ADD = 0x10000;
using namespace StompedReality::components;
SUITE ( Patch_Component_Test )
{
  TEST ( Create_Json_Test )
  {
    StompedReality::TYPE_ID riTypeID =
      StompedReality::type_system::TypeSystem::GetTypeID<RenderInfoComponent>( );
    StompedReality::type_system::TypeInfo* ricType =
      StompedReality::type_system::TypeSystem::GetType<RenderInfoComponent>( );
    StompedReality::TYPE_ID patchTypeID =
      StompedReality::type_system::TypeSystem::GetTypeID<PatchComponent>( );
    StompedReality::type_system::TypeInfo* patchType =
      StompedReality::type_system::TypeSystem::GetType< PatchComponent >( );

    //	std::cout << ricType->to_s() << std::endl;
    //	std::cout << patchType->to_s() << std::endl;
    StompedReality::Core::ComponentObjectPool cmps;
    StompedReality::Game g;
    cmps.SetGame( &g );

    cmps.AddObjectPool ( riTypeID,
                         ( StompedReality::Core::MemoryBlock<StompedReality::Core::Component>* )
                         StompedReality::Core::ObjectPool<RenderInfoComponent>::CreateObjectPool ( 5 ) );
    cmps.AddObjectPool ( patchTypeID,
                         ( StompedReality::Core::MemoryBlock<StompedReality::Core::Component>* )
                         StompedReality::Core::ObjectPool<PatchComponent>::CreateObjectPool ( 5 ) );
    StompedReality::ID cmpAId = cmps.CreateObject ( patchTypeID );
    PatchComponent* cmpA = ( PatchComponent* ) cmps.GetObject ( patchTypeID, cmpAId );
    CHECK_EQUAL ( 0, cmpA->GetPatchID( ) );
    cmpA->SetPatchID ( 3 );
    CHECK_EQUAL ( 3, cmpA->GetPatchID( ) );
    PatchComponent* cmpACheck = ( PatchComponent* ) cmps.GetObject ( patchTypeID, cmpAId );
    CHECK_EQUAL ( 3, cmpACheck->GetPatchID( ) );
    Json::Value root;
    patchType->write ( root, ( char* ) cmpA );

    //std::cout << "Patch node" << std::endl;
    //std::cout << root;
    StompedReality::ID cmpBId = cmps.CreateObject ( patchTypeID );
    PatchComponent* cmpB = ( PatchComponent* ) cmps.GetObject ( patchTypeID, cmpBId );
    patchType->read ( root, ( char* ) cmpB );
    CHECK_EQUAL ( cmpA->GetPatchID( ), cmpB->GetPatchID( ) );
  }
  TEST ( Render_Key_Test )
  {
    StompedReality::TYPE_ID riTypeID =
      StompedReality::type_system::TypeSystem::GetTypeID<RenderInfoComponent>( );
    StompedReality::type_system::TypeInfo* ricType =
      StompedReality::type_system::TypeSystem::GetType<RenderInfoComponent>( );
    StompedReality::TYPE_ID patchTypeID =
      StompedReality::type_system::TypeSystem::GetTypeID<PatchComponent>( );
    StompedReality::type_system::TypeInfo* patchType =
      StompedReality::type_system::TypeSystem::GetType< PatchComponent >( );
    StompedReality::Core::ComponentObjectPool cmps;
    StompedReality::Game g;
    cmps.SetGame( &g );

    cmps.AddObjectPool ( riTypeID,
                         ( StompedReality::Core::MemoryBlock<StompedReality::Core::Component>* )
                         StompedReality::Core::ObjectPool<RenderInfoComponent>::CreateObjectPool ( 5 ) );
    cmps.AddObjectPool ( patchTypeID,
                         ( StompedReality::Core::MemoryBlock<StompedReality::Core::Component>* )
                         StompedReality::Core::ObjectPool<PatchComponent>::CreateObjectPool ( 5 ) );
    StompedReality::ID cmpPatchId = cmps.CreateObject ( patchTypeID );
    PatchComponent* cmpPatch = ( PatchComponent* ) cmps.GetObject ( patchTypeID, cmpPatchId );
    cmpPatch->SetPatchID ( 3 );

    cmpPatch->SetID ( 87 );
    CHECK_EQUAL ( 3, cmpPatch->GetPatchID( ) );
    StompedReality::ID idRI = cmps.CreateObject ( riTypeID );
    RenderInfoComponent* cmpRI = ( RenderInfoComponent* ) cmps.GetObject (
        riTypeID,
        idRI );
    cmpRI->SetID ( 2 );
    cmpRI->SetDepth ( 0.3f );
    cmpRI->SetLayer ( 2 );
  }
}